package cn.good.controller;

import cn.good.domain.po.User;
import cn.good.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * TODO
 *
 * @Description
 * @Author wkm
 * @Date 2024/11/5
 **/
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @GetMapping("/findAll")
    public ModelAndView findAll(){
        ModelAndView modelAndView = new ModelAndView("UserList");
        List<User> users = userService.queryAllUsers();
//        System.out.println(users);
        modelAndView.addObject("mydata",users);
        return modelAndView;
    }

    @RequestMapping("getUser/{id}")
    public ModelAndView findonly(@PathVariable("id") int id){
        ModelAndView modelAndView = new ModelAndView("UserOnly");
        User user = userService.queryUserOnly(id);
        modelAndView.addObject("mydata",user);
        return modelAndView;
    }
    @RequestMapping ("/addUser/*")
    public ModelAndView addUser(User user){
        ModelAndView modelAndView = new ModelAndView("AddUser");
        int result = userService.addUser(user);
        modelAndView.addObject("mydata",user);
        return modelAndView;
    }
    @RequestMapping("/delete/{id}")
    public ModelAndView deleteUser(@PathVariable("id") int id){
        ModelAndView modelAndView = new ModelAndView("DeleteUser");
        User user = userService.queryUserOnly(id);
        int result = userService.removeUser(id);
        modelAndView.addObject("mydata",user);
        return modelAndView;
    }
    @RequestMapping("/updateUser/*")
    public ModelAndView updateUser(User user){
        ModelAndView modelAndView = new ModelAndView("UpdateUser");
        int result = userService.refreshUser(user);
        modelAndView.addObject("mydata",user);
        return modelAndView;
    }
}
